package webService;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * log out from the website
 * @author ran, barak
 *
 */
public class LogOutServlet extends HttpServlet{
	/**
	 * Default serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// get the session
		HttpSession session = req.getSession();
		session.setAttribute("logedIn", false);
		// get the cookies
		Cookie[] cookiesList = req.getCookies();
		System.out.println("cookiesList size : "+cookiesList.length);
		// delete the cookie
		for(Cookie c : cookiesList){
			if(c.getName().equals("cleverDropBoxCookie")){
				System.out.println("in loop of cookie");
				c.setMaxAge(0);
				c.setValue(null);
				resp.addCookie(c);
			}
		}
		// redirect to the index
		resp.sendRedirect(resp.encodeRedirectURL("index.jsp"));
	}
}
